package com.changgou.pay.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.pay.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/weixin/pay")

public class WeixinPayController {
    @Autowired
    private WeixinPayService weixinPayService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /***
     * 创建二维码
     * 普通订单 : "exchange.order","queue.order"
     * 秒杀订单 : "exchange.seckillorder","queue.seckillorder"
     * @return
     */
    @RequestMapping(value = "/create/native")
    public Result createNative(@RequestParam Map<String,String> parameterMap){
        Map<String,String> resultMap = weixinPayService.createNative(parameterMap);
        return new Result(true, StatusCode.OK,"创建二维码预付订单成功！",resultMap);
    }
    /***
     * 查询支付状态
     * @param outtradeno
     * @return
     */
    @GetMapping(value = "/status/query")
    public Result queryStatus(String outtradeno){
        Map<String,String> resultMap = weixinPayService.queryPayStatus(outtradeno);
        return new Result(true,StatusCode.OK,"查询状态成功！",resultMap);
    }
    /***
     * 支付回调
     * @param request
     * @return
     */
    @RequestMapping(value = "/notify/url")
    public String notifyUrl(HttpServletRequest request) throws Exception {
        //微信会返回网络输入流
        ServletInputStream inputStream = request.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //创建字节缓冲区
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer))!= -1){
            byteArrayOutputStream.write(buffer,0,len);
        }
        //微信支付结果的字节数组
        byte[] bytes = byteArrayOutputStream.toByteArray();
        String xmlResult = new String(bytes,"UTF-8");
        Map<String, String> resultMap = WXPayUtil.xmlToMap(xmlResult);
        //判断订单类型,是秒杀订单还是普通订单,二者队列不一致
        String attach = resultMap.get("attach");
        Map<String,String> attachMap = JSON.parseObject(attach,Map.class);
        //发送给MQ,有待修改
//        rabbitTemplate.convertAndSend("exchange.order","queue.order", JSON.toJSONString(resultMap));
        rabbitTemplate.convertAndSend(attachMap.get("exchange"),attachMap.get("routingkey"), JSON.toJSONString(resultMap));
        return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }
}
